package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.BannerDtoMapper;
import com.starlink.model.domain.BannerDO;
import com.starlink.mapper.BannerMapper;
import com.starlink.model.request.BannerRequest;
import com.starlink.service.BannerService;
import com.starlink.model.util.BannerConvert;
import com.starlink.model.vo.BannerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, BannerDO> implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public BannerVO getByKey(long id) {
        BannerVO bannerVO = BannerDtoMapper.INSTANCE.do2VO(getById(id));
        return bannerVO;
    }

    @Override
    public PageInfo<BannerVO> pageQuery(Pageable pageable,BannerRequest request) {
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<BannerDO> list = bannerMapper.pageQuery(request);
        List<BannerVO> bannerVOList = BannerConvert.buildBannerVoList(list);
        return new PageInfo(bannerVOList);
    }

    @Override
    public List<BannerVO> listQuery(BannerRequest request) {
        List<BannerDO> list = bannerMapper.selectDOList(BannerDtoMapper.INSTANCE.request2DO(request));
        List<BannerVO> bannerVOS = BannerConvert.buildBannerVoList(list);
        return bannerVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BannerRequest request) {
        save(BannerDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(BannerRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(BannerDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        bannerMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}